package com.yoomiito.app.ui.anyview.sureorder.header

import android.content.Context
import android.text.SpannableString
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.model.bean.AddressInfo
import com.yoomiito.app.model.my.MyRemainMoneyInfo
import com.yoomiito.app.ui.anyview.order.DeductionView
import com.yoomiito.app.ui.anyview.order.ShowAddressView
import com.yoomiito.app.utils.DensityUtil
import kotlinx.android.synthetic.main.item_order_num.view.*

abstract class BaseSureOrderHeaderView (val mContext: Context,var onShowListener:(spanText: SpannableString, goodsSize:Int, yoMoneyPay:Int)->Unit={ _, _, _->}): LinearLayout(mContext){
    private val addressView = ShowAddressView(mContext)
    val deductionView = DeductionView(mContext)
    var mGoodsSize = 1
    var mGoodsTotalPrice = "0"
    private var msgTv:TextView? = null

    init {
        orientation = VERTICAL
        val lp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        val v12 = DensityUtil.dp2px(12f)
        lp.leftMargin = v12
        lp.rightMargin = v12
        lp.topMargin = v12
        addView(addressView,lp)
    }

    abstract fun setMoneyInfo(moneyInfo: MyRemainMoneyInfo)
    abstract fun recalculateMoney()

    fun updateAddressInfo(addressInfo: AddressInfo){
        if (!addressInfo.id.isNullOrEmpty()){
            addressInfo.areaName = addressInfo.provinceName + addressInfo.cityName + addressInfo.countyName
            addressView.setAddressInfo(addressInfo)
            addressView.showAddress()
        }
    }

    /**
     * 商品数量控制view
     */
    fun addGoodsSizeView(){
        val sizeView = LayoutInflater.from(mContext).inflate(R.layout.item_order_num,null,false)
        val sizelp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        sizelp.topMargin = DensityUtil.dp2px(10f)
        sizelp.leftMargin=DensityUtil.dp2px(12f)
        sizelp.rightMargin=DensityUtil.dp2px(12f)
        addView(sizeView,sizelp)
        num_control_view.setListener { num: Int ->
            mGoodsSize = num
            recalculateMoney()
        }
        num_control_view.addOnInputNumChangeListener { num: Int ->
            mGoodsSize = num
            recalculateMoney()
        }
    }

    fun addDeductView(){
        val lp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        lp.topMargin = DensityUtil.dp2px(10f)
        lp.leftMargin=DensityUtil.dp2px(12f)
        lp.rightMargin=DensityUtil.dp2px(12f)
        addView(deductionView,lp)
    }

    /**
     * 备注view
     */
    fun  addMessageView(){
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_remark,null,false)
        val lp = LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)
        lp.topMargin = DensityUtil.dp2px(10f)
        lp.leftMargin=DensityUtil.dp2px(12f)
        lp.rightMargin=DensityUtil.dp2px(12f)
        addView(view,lp)
        msgTv = view.findViewById(R.id.act_buy_goods_notes)
    }

    fun msg():String = msgTv?.text?.toString() ?: ""

    fun addressInfo():AddressInfo = addressView.getmAddressInfo()?:AddressInfo()
}